//
//  HandleNetLogin.m
//  FinancialProduct
//
//  Created by Stella on 2017/8/28.
//  Copyright © 2017年 maq. All rights reserved.
//
#import "NetworkConfig.h"
#import "SVProgressHUD.h"
#import "NSDictionary+NN.h"
#import "HandleNetLogin.h"
#import "Header.h"
#import "TKAlertCenter.h"

@implementation HandleNetLogin
#pragma mark -获取验证码 
+ (void)getCodeForPhone:(NSString *)phone Success:(void(^)(NSDictionary *data))success Failure:(void(^)(NSString *errorInfo))failure{
    [HandleNetLogin show];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer    = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer.timeoutInterval = HTTP_REQUEST_TIMEOUT;
    NSDictionary  *params=@{@"AAAMethodName":@"Verificationcode",@"Phone":phone};

    [manager POST:API_HOST1 parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *data = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        [SVProgressHUD dismiss];
        NSLog(@"获取验证码返回： %@",[NSDictionary logDic:data]);
        NSArray *dataA=data[@"data"];
        if ([data[@"state"] intValue]==0) {
         
            
            if ([data[@"data"] isKindOfClass:[NSArray class]] && [data[@"data"] count] >0) {
                success(dataA[0]);
            }else{
                failure(HTTP_REQUEST_FAILURE);
               [[TKAlertCenter defaultCenter]postAlertWithMessage:HTTP_REQUEST_FAILURE];
            }
            
        }else{
            failure(data[@"msg"]);
            [[TKAlertCenter defaultCenter]postAlertWithMessage:data[@"msg"]];
        }

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(HTTP_REQUEST_FAILURE);
        [SVProgressHUD dismiss];
        [[TKAlertCenter defaultCenter]postAlertWithMessage:HTTP_REQUEST_FAILURE];
        
    }];

}
#pragma mark -登录检测
+ (void)CheckLoginForPhone:(NSString *)phone Password:(NSString *)password isShow:(BOOL)show Success:(void(^)(NSDictionary *data))success Failure:(void(^)(NSString *errorInfo))failure{
    if (show) {
       [HandleNetLogin show];
    }
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer    = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer.timeoutInterval = HTTP_REQUEST_TIMEOUT;
    NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    version = [version stringByReplacingOccurrencesOfString:@"." withString:@""];
    NSDictionary    *params=@{@"Phone":phone,@"Password":password,@"AAAMethodName":@"LoginCheck",
                              @"VersionNo":version};
    NSLog(@"登录检查： %@",[NSDictionary logDic:params]);
    [manager POST:API_HOST1 parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *data = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        NSLog(@"登录检查返回： %@",[NSDictionary logDic:data]);
        [SVProgressHUD dismiss];
        NSArray *dataA=data[@"data"];
        /***
         data =     (
         {
         Accountstatus = 2;//1 正式客户  2 临时客户
         CustomerID = 0;
         }
         );
         msg = "登录成功";
         state = 0;
         ***/
        
        if ([data[@"state"] intValue]==0) {
            
            
            if ([data[@"data"] isKindOfClass:[NSArray class]] && [data[@"data"] count] >0) {
                success(dataA[0]);
            }else{
                failure(HTTP_REQUEST_FAILURE);
                [[TKAlertCenter defaultCenter]postAlertWithMessage:HTTP_REQUEST_FAILURE];
            }
            
        }else{
            failure(data[@"msg"]);
            [[TKAlertCenter defaultCenter]postAlertWithMessage:data[@"msg"]];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(HTTP_REQUEST_FAILURE);
        [SVProgressHUD dismiss];
        [[TKAlertCenter defaultCenter]postAlertWithMessage:HTTP_REQUEST_FAILURE];
        
    }];
    
}
#pragma mark -客户基本信息检查（注册1/3）
+ (void)CheckRegisterWithName:(NSString *)name IDType:(NSString *)midType IDNo:(NSString *)midNo Success:(void(^)(NSDictionary *data))success Failure:(void(^)(NSString *errorInfo))failure{
    [HandleNetLogin show];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer    = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer.timeoutInterval = HTTP_REQUEST_TIMEOUT;
 
    NSDictionary *params=@{@"AAAMethodName":@"RegisterCheck",@"AccountUserName":name,@"IDDocumentType":midType,@"IDNumber":midNo};

    //NSLog(@"客户基本信息： params%@",params);
    
    [manager POST:API_HOST1 parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
        [SVProgressHUD dismiss];
        NSDictionary *data = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        /***返回消息，三种情况：1.已经注册客户，2.发送初始密码，3.新注册客户
         0代表通过，1代表失败
         **/
        //NSLog(@"客户基本信息检查返回： %@",[NSDictionary logDic:data]);
     
        if ([data[@"state"] intValue]==0) {
            
            success(data);
            
        }else{
            failure(data[@"msg"]);
            [[TKAlertCenter defaultCenter]postAlertWithMessage:data[@"msg"]];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [SVProgressHUD dismiss];
        [[TKAlertCenter defaultCenter]postAlertWithMessage:HTTP_REQUEST_FAILURE];
    }];

    
}
#pragma mark -客户获取手机验证码（注册2/3）
+ (void)RegisterUsePhone:(NSString *)phone andName:(NSString *)name Success:(void(^)(NSDictionary *data))success Failure:(void(^)(NSString *errorInfo))failure{
    [HandleNetLogin show];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer    = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer.timeoutInterval = HTTP_REQUEST_TIMEOUT;
    
    NSDictionary *params=@{@"AAAMethodName":@"VerificationCodeReg",@"AccountUserName":name,@"AccountPhone":phone};
    
    //NSLog(@"客户获取手机验证码： params%@",params);
    
    [manager POST:API_HOST1 parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
        [SVProgressHUD dismiss];
        NSDictionary *data = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        /***返回消息，三种情况：1.手机号码格式不正确，2.此号码已经被使用，3.验证码发送
         0代表通过，1代表失败
         **/
        //NSLog(@"客户获取手机验证码返回： %@",[NSDictionary logDic:data]);
        
        NSArray *dataA=data[@"data"];
        if ([data[@"state"] intValue]==0) {
            
            if ([data[@"data"] isKindOfClass:[NSArray class]] && [data[@"data"] count]> 0) {
                success(dataA[0]);
            }else{
                failure(HTTP_REQUEST_FAILURE);
               [[TKAlertCenter defaultCenter]postAlertWithMessage:HTTP_REQUEST_FAILURE];
            }
        }else{
            failure(data[@"msg"]);
            [[TKAlertCenter defaultCenter]postAlertWithMessage:data[@"msg"]];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [SVProgressHUD dismiss];
        [[TKAlertCenter defaultCenter]postAlertWithMessage:HTTP_REQUEST_FAILURE];
    }];
    
    
}
#pragma mark -客户设置密码（注册3/3）
+ (void)RegisterSetPassWithName:(NSString *)name Phone:(NSString *)phone PassWord:(NSString *)pass IDType:(NSString *)midType IDNo:(NSString *)midNo Success:(void(^)(NSDictionary *data))success Failure:(void(^)(NSString *errorInfo))failure{
    [HandleNetLogin show];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer    = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer.timeoutInterval = HTTP_REQUEST_TIMEOUT;
    
    NSDictionary *params=@{@"AAAMethodName":@"PasswordUpdate",
                           @"AccountUserName":name,
                           @"AccountPhone":phone,
                           @"AccountPassWord":pass,
                           @"IDDocumentType":midType,
                           @"IDNumber":midNo};
    
    //NSLog(@"客户设置密码： params%@",params);
    
    [manager POST:API_HOST1 parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
        [SVProgressHUD dismiss];
        NSDictionary *data = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        /***返回消息，三种情况：1.手机号码格式不正确，2.此号码已经被使用，3.验证码发送
         0代表通过，1代表失败
         **/
        //NSLog(@"客户设置密码返回： %@",[NSDictionary logDic:data]);
//        NSArray *dataA=data[@"data"];
        if ([data[@"state"] intValue]==0) {
            
        
            success(data);
            
        }else{
            failure(data[@"msg"]);
            [[TKAlertCenter defaultCenter]postAlertWithMessage:data[@"msg"]];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [SVProgressHUD dismiss];
        [[TKAlertCenter defaultCenter]postAlertWithMessage:HTTP_REQUEST_FAILURE];
    }];
    
    
}

#pragma mark -客户修改密码
+ (void)ChangePassWithPhone:(NSString *)phone PassWord:(NSString *)pass NewPassWord:(NSString *)pass2 Success:(void(^)(NSDictionary *data))success Failure:(void(^)(NSString *errorInfo))failure{
    [HandleNetLogin show];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer    = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer.timeoutInterval = HTTP_REQUEST_TIMEOUT;
    
    NSDictionary *params=@{@"AAAMethodName":@"ChangePassword",
                           @"OldPassword":pass,
                           @"Phone":phone,
                           @"NewPassword":pass2};
    [manager POST:API_HOST1 parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
        [SVProgressHUD dismiss];
        NSDictionary *data = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        /***返回消息，三种情况：1.手机号码格式不正确，2.此号码已经被使用，3.验证码发送
         0代表通过，1代表失败
         **/
        //NSLog(@"客户修改密码返回： %@",[NSDictionary logDic:data]);
        
        if ([data[@"state"] intValue]==0) {
            
            success(data);
//            [SVProgressHUD showInfoWithStatus:@"修改成功"];
        }else{
            failure(data[@"msg"]);
            [[TKAlertCenter defaultCenter]postAlertWithMessage:data[@"msg"]];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [SVProgressHUD dismiss];
        [[TKAlertCenter defaultCenter]postAlertWithMessage:HTTP_REQUEST_FAILURE];
    }];
    
    
}

#pragma mark -重置密码(1/3)
+ (void)retrieveUserName:(NSString *)username AccountPhone:(NSString *)phone Success:(void(^)(NSDictionary *data))success Failure:(void(^)(NSString *errorInfo))failure{

    
    [HandleNetLogin show];
 
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer    = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer.timeoutInterval = HTTP_REQUEST_TIMEOUT;
    NSDictionary    *params=@{@"AccountName":username,@"AccountPhone":phone,@"AAAMethodName":@"RetrievePasswordUserinfo",};
    
    [manager POST:API_HOST1 parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *data = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        NSLog(@"重置密码(1/3)返回： %@",[NSDictionary logDic:data]);
        [SVProgressHUD dismiss];
//        NSArray *dataA=data[@"data"];
        if ([data[@"state"] intValue]==0) {
            /***
            
             ***/
            
            success(data);
            
        }else{
            failure(data[@"msg"]);
            [[TKAlertCenter defaultCenter]postAlertWithMessage:data[@"msg"]];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(HTTP_REQUEST_FAILURE);
        [SVProgressHUD dismiss];
        [[TKAlertCenter defaultCenter]postAlertWithMessage:HTTP_REQUEST_FAILURE];
        
    }];
    
}
#pragma mark -重置密码(2/3)----获取验证码
+ (void)retrieveCodeWithUserName:(NSString *)username AccountPhone:(NSString *)phone Success:(void(^)(NSDictionary *data))success Failure:(void(^)(NSString *errorInfo))failure{
    [HandleNetLogin show];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer    = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer.timeoutInterval = HTTP_REQUEST_TIMEOUT;
    NSDictionary    *params=@{@"AccountName":username,@"AccountPhone":phone,@"AAAMethodName":@"RetrievePasswordVerificationcode",};
    
    [manager POST:API_HOST1 parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *data = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        NSLog(@"重置密码(2/3)返回： %@",[NSDictionary logDic:data]);
        [SVProgressHUD dismiss];
        NSArray *dataA=data[@"data"];
        if ([data[@"state"] intValue]==0) {
            /***
             data =     (
             {
             VerificationCode = 734425;
             }
             );
             msg = "成功";
             state = 0;
             ***/
            if ([data[@"data"] isKindOfClass:[NSArray class]] && [data[@"data"] count]>0) {
                success(dataA[0]);
            }else{
                failure(HTTP_REQUEST_FAILURE);
                [[TKAlertCenter defaultCenter]postAlertWithMessage:HTTP_REQUEST_FAILURE];
            }
            
            
        }else{
            failure(data[@"msg"]);
            [[TKAlertCenter defaultCenter]postAlertWithMessage:HTTP_REQUEST_FAILURE];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(HTTP_REQUEST_FAILURE);
        [SVProgressHUD dismiss];
        [[TKAlertCenter defaultCenter]postAlertWithMessage:HTTP_REQUEST_FAILURE];
        
    }];
    
}
#pragma mark -重置密码(3/3)----设置密码
+ (void)retrieveReSetPassWithUserName:(NSString *)username AccountPhone:(NSString *)phone PassWord:(NSString *)password Success:(void(^)(NSDictionary *data))success Failure:(void(^)(NSString *errorInfo))failure{
    [HandleNetLogin show];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer    = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer.timeoutInterval = HTTP_REQUEST_TIMEOUT;
    NSDictionary    *params=@{@"AccountName":username,
                              @"AccountPhone":phone,
                              @"AAAMethodName":@"RetrievePasswordSetNewPassword",
                              @"PassWord":password};
    NSLog(@"重置密码(3/3)params---%@",params);
    [manager POST:API_HOST1 parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *data = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        NSLog(@"重置密码(3/3)返回： %@",[NSDictionary logDic:data]);
        [SVProgressHUD dismiss];
//        NSArray *dataA=data[@"data"];
        if ([data[@"state"] intValue]==0) {
            /***
             
             ***/
            
            success(data);
            
        }else{
            failure(data[@"msg"]);
            [[TKAlertCenter defaultCenter]postAlertWithMessage:data[@"msg"]];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(HTTP_REQUEST_FAILURE);
        [SVProgressHUD dismiss];
        [[TKAlertCenter defaultCenter]postAlertWithMessage:HTTP_REQUEST_FAILURE];
        
    }];
    
}

#pragma mark -获取公司名字
+ (void)getCompanyNameSuccess:(void(^)(NSDictionary *data))success Failure:(void(^)(NSString *errorInfo))failure{
//    [HandleNetLogin show];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer    = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer.timeoutInterval = HTTP_REQUEST_TIMEOUT;
    NSDictionary    *params=@{@"AAAMethodName":@"SelectCompany"};
    
    [manager POST:API_HOST1 parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *data = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        NSLog(@"获取公司名字： %@",[NSDictionary logDic:data]);
//        [SVProgressHUD dismiss];
                NSArray *dataA=data[@"data"];
        if ([data[@"state"] intValue]==0 &&dataA.count==1) {
            /***
             
             ***/
            
            
            if ([data[@"data"] isKindOfClass:[NSArray class]] && [data[@"data"] count]>0) {
                success(dataA[0]);
            }else{
                failure(HTTP_REQUEST_FAILURE);
//                [[TKAlertCenter defaultCenter]postAlertWithMessage:HTTP_REQUEST_FAILURE];
            }
            
        }else{
            failure(data[@"msg"]);
//            [[TKAlertCenter defaultCenter]postAlertWithMessage:data[@"msg"]];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(HTTP_REQUEST_FAILURE);
//        [SVProgressHUD dismiss];
//        [[TKAlertCenter defaultCenter]postAlertWithMessage:HTTP_REQUEST_FAILURE];
        
    }];
}

+(void)show{
    [SVProgressHUD showInfoWithStatus:@"loading..."];
}

+(void)showProgress:(float)gress{
    [SVProgressHUD showProgress:gress];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
}
@end
